How to get Ringtone name in Android?
Asked Answered
M

3

22

I'm allowing my user to pick a ringtone for notifications in my app. I want to store the URI of the sound along with the human readable title of the sound.

So far the URI code works great:

Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

But when I try to get the title, and set it as a button text, I don't get anything. Seems to have no title?

String title = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
button.setText(title);

But my button text is empty. If I do:

button.setText(uri.toString());

then I see the uri perfectly. Should I just try to get the title from the URI? Thanks

Mcnully answered 4/10, 2013 at 18:1 Comment(0)
L
54

This should get it:

Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
String title = ringtone.getTitle(this);

Refer to http://developer.android.com/reference/android/media/Ringtone.html for the documentation, but the short story: Ringtone.getTitle(Context ctx);

Largehearted answered 4/10, 2013 at 18:9 Comment(2)
Thanks. Docs always seem to get me.Mcnully
+Alexey Kuznetsov what do you mean ?Wertheimer
L
3

I personally had a serious performance problem when I tried the accepted answer, it took about 2 seconds to just load a list of 30 ringtones. I changed it a bit and it works about 10x faster:

uri = ringtoneMgr.getRingtoneUri(cursor.getPosition());
ContentResolver cr = getContext().getContentResolver();
String[] projection = {MediaStore.MediaColumns.TITLE};
String title;
Cursor cur = cr.query(uri, projection, null, null, null);
    if (cur != null) {
        if (cur.moveToFirst()) {
            title = cur.getString(0);
            cur.close();
Luxuriate answered 28/3, 2017 at 13:33 Comment(0)
B
0

I had problems with 'MediaPlayer finalized without being released'. I use this:

Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
returnCursor.moveToFirst();
String title = returnCursor.getString(returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
returnCursor.close();

Refer to https://developer.android.com/training/secure-file-sharing/retrieve-info.html for the documentation.

Blackmon answered 19/8, 2015 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.